package com.aliwo.service.impl;

import com.aliwo.service.JobCommonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tech.powerjob.client.PowerJobClient;
import tech.powerjob.common.request.query.JobInfoQuery;
import tech.powerjob.common.response.InstanceInfoDTO;
import tech.powerjob.common.response.JobInfoDTO;
import tech.powerjob.common.response.ResultDTO;

import java.util.List;
import java.util.stream.Collectors;


/**
 * @author:xuyayuan
 * @date:2021/7/26 13:35
 * @description:TODO
 */
@SuppressWarnings("all")
@Service
@Transactional(readOnly = true, rollbackFor = Exception.class)
public class JobCommonServiceImpl implements JobCommonService {

    @Autowired
    private PowerJobClient powerJobClient;

    //@Autowired
    //private JobInfoRepository jobInfoRepository;

    //@Override
    //public BaseRepository<JobInfoDO, String> getBaseRepository() {
    //    return jobInfoRepository;
    //}


    /**
     * 功能描述:TODO
     * @author xuyayuan
     * @param queryJobInfoRequestrequest
     * @param pagingParam
     * @return org.springframework.data.domain.Page<com.aliwo.remote.model.JobInfoDO>
     */
    //@Override
    //public Page<JobInfoDO> findPage(QueryJobInfoRequest queryJobInfoRequestrequest, PagingParam pagingParam) {
    //    List<SearchFilter> searchFilters = new ArrayList<>();
    //    PageRequest pageRequest = PageRequestUtils.buildPageRequest(pagingParam);
    //    // 无查询条件，查询全部
    //    if(queryJobInfoRequestrequest.getJobId() == null && StringUtils.isEmpty(queryJobInfoRequestrequest
    //    .getKeyword())){
    //        Page<JobInfoDO> result = jobInfoRepository.findByAppIdAndStatusNot(queryJobInfoRequestrequest.getAppId(),
    //                SwitchableStatus.DELETED.getV(), pageRequest);
    //        return result;
    //        //searchFilters.add(SearchFilter.equal("appId", queryJobInfoRequestrequest.getAppId()));
    //        //searchFilters.add(SearchFilter.equal("jobId", queryJobInfoRequestrequest.getJobId()));
    //        //List<SearchFilter> likeFilters = new ArrayList<>();
    //    }
    //
    //
    //    //PageRequest pageRequest = PageRequestUtils.buildPageRequest(pagingParam);
    //    //Specification<JobInfoDO> spec = JpaSearchUtils.buildAndSpec(searchFilters);
    //    return null;
    //}

    /**
     * 功能描述:复制任务
     *
     * @param jobId
     * @return tech.powerjob.common.response.ResultDTO<java.lang.Long>
     * @author xuyayuan
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultDTO<Long> copyJob(Long jobId) {
        return powerJobClient.copyJob(jobId);
    }

    /**
     * 功能描述:查找任务
     *
     * @param jobId
     * @return tech.powerjob.common.response.ResultDTO<tech.powerjob.common.response.JobInfoDTO>
     * @author xuyayuan
     */
    @Override
    public ResultDTO<JobInfoDTO> fetchJob(Long jobId) {
        return powerJobClient.fetchJob(jobId);
    }

    /**
     * 功能描述:查询该分组下所有任务
     *
     * @param
     * @return tech.powerjob.common.response.ResultDTO<java.util.List < tech.powerjob.common.response.JobInfoDTO>>
     * @author xuyayuan
     */
    @Override
    public ResultDTO<List<JobInfoDTO>> fetchAllJob() {
        ResultDTO<List<JobInfoDTO>> jobInfoDTOList = powerJobClient.fetchAllJob();
        return jobInfoDTOList;
    }

    /**
     * 功能描述:查询该分组下所有任务不包含删除的任务
     *
     * @param
     * @return tech.powerjob.common.response.ResultDTO<java.util.List < tech.powerjob.common.response.JobInfoDTO>>
     * @author xuyayuan
     */
    @Override
    public ResultDTO<List<JobInfoDTO>> fetchAllJobExcludeDelete() {
        ResultDTO<List<JobInfoDTO>> jobInfoDTOList = powerJobClient.fetchAllJob();
        jobInfoDTOList.setData(jobInfoDTOList.getData().stream()
                .filter(jobInfoDTO -> jobInfoDTO.getStatus() != 99).collect(Collectors.toList()));
        return jobInfoDTOList;
    }

    /**
     * 功能描述:根据查询条件，查询任务信息
     *
     * @param powerQuery
     * @return tech.powerjob.common.response.ResultDTO<java.util.List < tech.powerjob.common.response.JobInfoDTO>>
     * @author xuyayuan
     */
    @Override
    public ResultDTO<List<JobInfoDTO>> queryJob(JobInfoQuery powerQuery) {
        ResultDTO<List<JobInfoDTO>> jobInfoDTOList = powerJobClient.queryJob(powerQuery);
        return jobInfoDTOList;
    }

    /**
     * 功能描述:禁用任务
     *
     * @param jobId
     * @return tech.powerjob.common.response.ResultDTO<java.lang.Void>
     * @author xuyayuan
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultDTO<Void> disableJob(Long jobId) {
        return powerJobClient.disableJob(jobId);
    }

    /**
     * 功能描述:启用某个任务
     *
     * @param jobId
     * @return tech.powerjob.common.response.ResultDTO<java.lang.Void>
     * @author xuyayuan
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultDTO<Void> enableJob(Long jobId) {
        return powerJobClient.enableJob(jobId);
    }

    /**
     * 功能描述:删除某个任务
     *
     * @param jobId
     * @return tech.powerjob.common.response.ResultDTO<java.lang.Void>
     * @author xuyayuan
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultDTO<Void> deleteJob(Long jobId) {
        return powerJobClient.deleteJob(jobId);
    }

    /**
     * 功能描述:运行某个任务（支持延迟执行）
     *
     * @param jobId
     * @param instanceParams
     * @param delayMS
     * @return tech.powerjob.common.response.ResultDTO<java.lang.Long>
     * @author xuyayuan
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultDTO<Long> runJob(Long jobId, String instanceParams, long delayMS) {
        return powerJobClient.runJob(jobId, instanceParams, delayMS);
    }

    /**
     * 功能描述:取消某个定时任务实例
     *
     * @param instanceId
     * @return tech.powerjob.common.response.ResultDTO<java.lang.Void>
     * @author xuyayuan
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultDTO<Void> cancelInstance(Long instanceId) {
        return powerJobClient.cancelInstance(instanceId);
    }

    /**
     * 功能描述:重试某个定时任务实例
     *
     * @param instanceId
     * @return tech.powerjob.common.response.ResultDTO<java.lang.Void>
     * @author xuyayuan
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultDTO<Void> retryInstance(Long instanceId) {
        return powerJobClient.retryInstance(instanceId);
    }

    /**
     * 功能描述:停止某个任务实例
     *
     * @param instanceId
     * @return tech.powerjob.common.response.ResultDTO<java.lang.Void>
     * @author xuyayuan
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultDTO<Void> stopInstance(Long instanceId) {
        return powerJobClient.stopInstance(instanceId);
    }

    /*
     * 功能描述:查询某个任务实例
     * @author xuyayuan
     * @param instanceId
     * @return tech.powerjob.common.response.ResultDTO<tech.powerjob.common.response.InstanceInfoDTO>
     */
    @Override
    public ResultDTO<InstanceInfoDTO> fetchInstanceInfo(Long instanceId) {
        System.out.println("fetchInstanceInfo : powerJobClient class " + powerJobClient.getClass()
                + "powerJobClient toString" + powerJobClient.toString());
        return powerJobClient.fetchInstanceInfo(instanceId);
    }

    /**
     * 功能描述:查询某个任务实例的状态
     *
     * @param instanceId
     * @return tech.powerjob.common.response.ResultDTO<java.lang.Integer>
     * @author xuyayuan
     */
    @Override
    public ResultDTO<Integer> fetchInstanceStatus(Long instanceId) {
        return powerJobClient.fetchInstanceStatus(instanceId);
    }
}
